package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.caucho.hessian.server.HessianServlet;
  
public class ServiceImpl extends HessianServlet implements IService {  
  
    private static final long serialVersionUID = 8385639368192939451L;  
  
    static Map<Long, Property> PROPERTY_MAP = new HashMap<Long, Property>();
    static {    	
    	Long id1 = 1L; 
    	Property property = new Property();
    	property.setB(true);
    	property.setI(11);
    	property.setL(22L);
    	property.setD(33.33);
    	property.setName("steven");  
    	
    	byte[] buffer = new byte[]{0x30,0x31};
    	property.setBuffer(buffer);
    	
    	Car carArray0 = new Car();
    	carArray0.setName("111Ferrari");
    	carArray0.setDesc("111roadster PC");
    	property.setCarArray(new Car[]{carArray0}); 
		
    	Map<String, Car> m = new HashMap<String, Car>();
    	Car carMap = new Car();
    	carMap.setName("Ferrari");
    	carMap.setDesc("roadster PC");
		m.put("steven", carMap);
		
		// car
		List<Car> cars = new ArrayList<Car>();
		Car car1 = new Car();
		car1.setName("Ferrari");
		car1.setDesc("roadster PC");
		
		Car car2 = new Car();
		car2.setName("wulingzhiguang ");
		car2.setDesc("MBC");
		cars.add(car1);
		cars.add(car2);	
		
		property.setM(m); 
 		
		// stock
		List<Stock> stocks = new ArrayList<Stock>();
		Stock stock1 = new Stock();
		stock1.setId(700);
		stock1.setDesc("Tencent Holdings Limited");
		stock1.setCount(100); 
		
		Stock stock2 = new Stock();
		stock2.setId(2628);
		stock2.setDesc("China Life");
		stock2.setCount(1000); 
		stocks.add(stock1);
		stocks.add(stock2);		
		
		property.setCars(cars); 
		property.setStocks(stocks);
		PROPERTY_MAP.put(id1, property);
		
		/////////////////////////////////////////////////////pop
		Long id2 = 2L; 
    	property = new Property();
		property.setName("pop");
		
		// car
		cars = new ArrayList<Car>();
		car1 = new Car();
		car1.setName("hyundai");
		car1.setDesc("car");		
		cars.add(car1);
		
		// stock
		stocks = new ArrayList<Stock>();
		stock1 = new Stock();
		stock1.setId(2318);
		stock1.setDesc("china pinan");
		stock1.setCount(1000); 		
		stocks.add(stock1);		
		
		property.setCars(cars);
		property.setStocks(stocks);
		PROPERTY_MAP.put(id2, property);
    }
    
    public String sayHello(String name) {  
        return "hello: " + name;  
    }    

	public Property getProperty(Person person, String desc) {
		List<Person> persons = new ArrayList<Person>(1);
		persons.add(person);
		List<Property> properties = getProperties(persons, desc);
		if (!properties.isEmpty()) {
			return properties.get(0);
		}
		return null;
	}

	public List<Property> getProperties(List<Person> persons, String desc) {
		System.out.println("getProperties: " + desc);
		
		List<Property> properties = new ArrayList<Property>();
		
		for (Person person: persons) {	
			LogUtils.printPerson(person); 
			
			Property property = PROPERTY_MAP.get(person.getUin());
			if (property != null) {
				properties.add(property);
			}
		}
		return properties;
	}   	
}   